package com.ftg.learn.chapter06;

import java.util.Arrays;

/**
 * 队列
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/7/14 上午9:40
 */
public class QueueForMe {

    /**
     * 成员变量
     */
    private int init_capacity = 10;
    private int max_capacity = Integer.MAX_VALUE;

    /**
     * 要先开启空间
     */
    private int[] bottom_array;

    /**
     * 索引 ： 用来存储数组元素时的下标
     */
    private int index = 0;

    public QueueForMe() {
        bottom_array = new int[init_capacity];
    }

    public boolean append(int x) {
        long newCapacity = bottom_array.length + (bottom_array.length >> 1);
        int lastCapacity = newCapacity > max_capacity ? max_capacity : (int) newCapacity;

        if (index == bottom_array.length) {
            bottom_array = Arrays.copyOf(bottom_array, lastCapacity);
        }
        bottom_array[index++] = x;
        return true;
    }

    public int getHeader() {
        // 容错  当index=0 ......
//        jdk提供的一个数组拷贝方法
        int t = bottom_array[0];
        if(index != 1){
            System.arraycopy(bottom_array, 1, bottom_array, 0, index - 1);
        }
        index--;
        return t;
    }

    public static void main(String[] args) {
        QueueForMe q = new QueueForMe();
        q.append(1);
        q.append(2);

        System.out.println(q.getHeader());
        System.out.println(q.getHeader());
    }


}
